package com.yue.jersey.practice.common;

import com.yue.jersey.practice.model.CountryModel;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 功能描述: TODO <br/>
 * 创建时间: 2016/12/2<br/>
 *
 * @author QiuYang
 */
public final class DatasUtils {
    private static final String COUNTRYPAH = "datas/country.txt";

    private static final List<String> countries = new ArrayList<>();

    static{
        InputStream countryInput = DatasUtils.class.getClassLoader().getResourceAsStream(COUNTRYPAH);
        readDatas(countryInput);
    }

    private static void readDatas(InputStream inputStream){
        InputStreamReader reader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(reader);
        String lineDatas = null;
        try {
            while (true) {
                lineDatas = bufferedReader.readLine();
                if (null == lineDatas) {
                    break;
                }

                if (!"".equals(lineDatas)) {
                    countries.add(lineDatas);
                } else {
                    continue;
                }
            }
        }catch(IOException e){
            throw new RuntimeException(e);
        }
    }

    public static List<String> getAllCountries() throws IOException {
        return countries;
    }

    public static List<CountryModel> allCountries() throws IOException {
        List<String> allCountries = getAllCountries();

        List<CountryModel> countryModels = new ArrayList<>();
        allCountries.stream()
                .forEach(country ->{
                    String sName = country.substring(0, 2);
                    String cName = country.substring(country.indexOf("-")+1, country.indexOf("("));
                    String eName = country.substring(country.indexOf("(")+1, country.indexOf(")"));
                    CountryModel model = new CountryModel();
                    model.setsName(sName);
                    model.setcName(cName);
                    model.seteName(eName);

                    countryModels.add(model);
                });
        return countryModels;
    }

    public static List<String> getCountries(String keyword) throws IOException {
        List<String> allCountries = getAllCountries();

        List<String> countries = allCountries.stream()
                .filter(country -> country.contains(keyword))
                .collect(Collectors.toList());

        return countries;
    }






    public static void main(String[] args) throws IOException {
//        URL url = DatasUtils.class.getClassLoader().getResource(COUNTRYPAH);
//        System.out.println(url.getHost() + "  :  "+ url.getPath() + "  :  " + url.getFile());


//        getAllCountries().forEach(System.out::println);
//        getCountries("中").forEach(System.out::println);
        allCountries().forEach(System.out::println);
    }
}
